package com.qcby.mycommunity_003.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qcby.mycommunity_003.annotation.LogAnnotation;
import com.qcby.mycommunity_003.entity.*;
import com.qcby.mycommunity_003.mapper.*;
import com.qcby.mycommunity_003.service.CommunityService;
import com.qcby.mycommunity_003.util.Result;
import com.qcby.mycommunity_003.vo.CommVo;
import com.qcby.mycommunity_003.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-03-25
 */
@RestController
@RequestMapping("/sys/community")
public class CommunityController {
    @Autowired
    private CommunityService communityService;
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private CameraMapper cameraMapper;
    @Autowired
    private InOutRecordMapper inOutRecordMapper;
    @Autowired
    private ManualRecordMapper manualRecordMapper;
    @Autowired
    private PersonMapper personMapper;
    /**
     * 社区分页条件查询
     * @param commVo
     * @return
     */
    @GetMapping("/list")
    public Result Commlist(CommVo commVo){
        PageVo pageVo= communityService.ComList(commVo);
        return Result.ok().put("data",pageVo);
    }

    /**
     * 添加小区
     * @param community
     * @return
     */
    @LogAnnotation("添加小区")
    @PostMapping("/add")
    public Result AddCommunity(@RequestBody Community community, HttpSession session){
//        //设置创建人
        User user = (User) session.getAttribute("user");
        community.setCreater(user.getUsername());
        //添加用户
        int insert = communityMapper.insert(community);
        if(insert>0){
            return Result.ok();
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 根据id查找，做回显用的。
     * url拼接获取参数，注意这个写法
     * @PathVariable注解作用是映射url绑定的占位符
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result SearchById(@PathVariable("id")Integer id){
        Community community = communityMapper.selectById(id);
        return Result.ok().put("data",community);
    }

    /**
     * 修改小区信息
     * @param community
     * @return
     */
    @LogAnnotation("修改小区信息")
    @PutMapping("/edit")
    public Result updateCommunity(@RequestBody Community community){
        QueryWrapper<Community>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("community_id",community.getCommunityId());
        communityMapper.update(community,queryWrapper);
        return Result.ok();
    }

    /**
     * 删除的时候要删除，相关联的数据
     * @param ids
     * @return
     * @Transactional框架提供的事务注解
     */
    @LogAnnotation("删除小区")
    @DeleteMapping("/del")
    @Transactional
    public Result delectCommunity(@RequestBody Integer[] ids){
        //相关联的表===摄像头、出入记录、访客记录、person表都要删除
       //有一个bug，就是如果这个小区没有摄像头，那会一直回滚，就做不了删除小区的操作
        //1.摄像头删除
        QueryWrapper<Camera> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.in("community_id",ids);
        Integer count1 = cameraMapper.selectCount(queryWrapper1);
        if(count1>0){
            cameraMapper.deleteBatchIds(Arrays.asList(ids));
        }
        //2.出入记录删除
        QueryWrapper<InOutRecord> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.in("community_id",ids);
        Integer count2 = inOutRecordMapper.selectCount(queryWrapper2);
        if(count2>0){
            cameraMapper.deleteBatchIds(Arrays.asList(ids));
        }
        //3.访客记录删除
        QueryWrapper<ManualRecord> queryWrapper3=new QueryWrapper<>();
        queryWrapper3.in("community_id",ids);
        Integer count3 = manualRecordMapper.selectCount(queryWrapper3);
        if(count3>0){
            manualRecordMapper.deleteBatchIds(Arrays.asList(ids));
        }
        //4.person删除
        //3.访客记录删除
        QueryWrapper<Person> queryWrapper4=new QueryWrapper<>();
        queryWrapper4.in("community_id",ids);
        Integer count4 = personMapper.selectCount(queryWrapper4);
        if(count4>0){
            personMapper.deleteBatchIds(Arrays.asList(ids));
        }
        //4.删除小区
        communityMapper.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

    /**
     * 获取地图，
     * @return
     */
    @GetMapping("/getCommunityMap")
    public Result getCommunityMap(){
        //只需要查询到所有的小区列表就行，返回前端。
        //调用百度地图的vue接口
        List<Community> list = communityService.list();
        return Result.ok().put("data",list);
    }
}

